package com.meixiang.adapter.collect;

import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.meixiang.R;
import com.meixiang.entity.collect.CollectDateEntity;
import com.meixiang.util.AbStrUtil;
import com.meixiang.view.OldPriceView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class SwipeAdapter extends BaseAdapter {
    /**
     * 上下文对象
     */
    private FragmentActivity activity = null;
    private List<CollectDateEntity> mCollectList=new ArrayList<>();

    private int mRightWidth = 0;

    /**
     * @param
     */
    public SwipeAdapter(FragmentActivity activity) {
        this.activity = activity;
    }
    public void addData(List<CollectDateEntity> mCollectList){
        this.mCollectList.addAll(mCollectList);
        notifyDataSetChanged();
    }

    public void clearData(){
        mCollectList.clear();
        notifyDataSetChanged();
    }
    public void removeData(int position){
        mCollectList.remove(position);
        notifyDataSetChanged();
    }
    public List<CollectDateEntity> getData(){
        return mCollectList;
    }
    @Override
    public int getCount() {
        return mCollectList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCollectList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {// 有直接获得ViewHolder
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(activity).load(mCollectList.get(position).getFavImage()).error(R.drawable.image_default_gray_bg)
                .placeholder(R.drawable.image_default_gray_bg)
                .into(holder.ivImg);
        holder.tvTitle.setText(mCollectList.get(position).getFavName());
        String price=mCollectList.get(position).getFavStorePrice();
        if (AbStrUtil.isEmpty(price)){
            holder.tvMoney.setText("¥ 0.00");
        }else if (Double.parseDouble(price)>=1){
            holder.tvMoney.setText("¥ " + Math.round(Double.parseDouble(price)));
        }else{
            holder.tvMoney.setText("¥ " + mCollectList.get(position).getFavStorePrice());
        }
        String marketPrice=mCollectList.get(position).getFavMarketPrice();
        if (AbStrUtil.isEmpty(marketPrice)){
            holder.tvOldPrice.setText("¥ 0.00");
        }else if (Double.parseDouble(marketPrice)>=1){
            holder.tvOldPrice.setText("¥ " + Math.round(Double.parseDouble(marketPrice)));
        }else{
            holder.tvOldPrice.setText("¥ " + mCollectList.get(position).getFavMarketPrice());
        }
        holder.tvBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onBtnItemClick(v, position);
                }
            }
        });

        if ("2".equals(mCollectList.get(position).getFavType())) { //商品收藏
            holder.tvBtn.setText("预约服务");
        } else if ("1".equals(mCollectList.get(position).getFavType())) {
            holder.tvBtn.setText("加入购物车");
        }

        holder.itemRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onRightItemClick(v, position);
                }
            }
        });
        return convertView;
    }


    /**
     * 单击事件监听器
     */
    private onRightItemClickListener mListener = null;

    public void setOnRightItemClickListener(onRightItemClickListener listener) {
        mListener = listener;
    }

    public interface onRightItemClickListener {
        /**
         * 取消收藏点击事件
         * @param v
         * @param position
         */
        void onRightItemClick(View v, int position);

        /**
         * 按钮点击事件
         * @param v
         * @param position
         */
        void onBtnItemClick(View v,int position);
    }

    static class ViewHolder {
        @Bind(R.id.iv_img)
        ImageView ivImg;
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_money)
        TextView tvMoney;
        @Bind(R.id.tv_old_price)
        OldPriceView tvOldPrice;
        @Bind(R.id.tv_btn)
        TextView tvBtn;
        @Bind(R.id.item_left)
        LinearLayout itemLeft;
        @Bind(R.id.item_right)
        LinearLayout itemRight;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
